package dataConstructions_finished;

public class T121 {

    public static void main(String[] args) {

        int[] ints = new int[] {7,1,5,3,6,4};

        int ret = maxProfit(ints);
        System.out.println(ret);
    }

    public static int maxProfit(int[] prices) {

        int len = prices.length;
        if (len == 1) {
            return 0;
        }

        int[] income = new int[len];
        income[0] = 0;
        int[] minNum = new int[len];
        minNum[0] = prices[0];

        for (int i = 1; i < len; i++) {
            //迭代最小股值
            minNum[i] = Math.min(prices[i], minNum[i - 1]);

            //迭代最大差值
            income[i] = Math.max(prices[i] - minNum[i], income[i - 1]);
        }

        return income[len-1];
    }

}
